package cn.bingo.yunsj.config.model;

import bingo.link.sdk.handler.uam.UamUserHandler;
import cn.bingo.yunsj.common.result.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Component("myAuthenticationSuccessHandler")
@CrossOrigin
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    private static final Logger logger= LoggerFactory.getLogger(MyAuthenticationSuccessHandler.class);

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
            throws IOException, ServletException {
        String url=request.getRequestURI();
        UamUserHandler uamUserHandler = new UamUserHandler();
        logger.info("url:"+url);
        //如果是要跳转到某个页面的
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Credentials","true");
        Cookie cookie = new Cookie("username",uamUserHandler.getCurrentUser().getName());
        response.addCookie(cookie);
    }
}
